ffi: throw on missing memory helper arguments - #65500
Conversation
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65500 +/- ##
==========================================
- Coverage 90.20% 90.19% -0.02%
==========================================
Files 771 771
Lines 264622 264619 -3
Branches 50228 50240 +12
==========================================
- Hits 238704 238660 -44
- Misses 16907 16947 +40
- Partials 9011 9012 +1
🚀 New features to boost your workflow:
|
ffi.getInt8() through ffi.getFloat64(), ffi.setInt8() through ffi.setFloat64(), ffi.toBuffer() and ffi.toArrayBuffer() return undefined instead of throwing when a required argument is omitted, so a call that read or wrote nothing cannot be told apart from one that read a zero byte. All 22 helpers behave this way. GetValidatedPointerAddress() and GetValidatedSize() already reject the same argument when it is passed explicitly as undefined. The args.Length() test in front of them short-circuits the call and returns Nothing without scheduling an exception. These six are the only tests in src/ where args.Length() can skip a call that throws; the only other Length() tests that guard a call at all guard Buffer::HasInstance(), which cannot throw. The remaining tests in this file guard an inline predicate and throw in the branch, which is why setUint8(ptr) reports "Expected an offset argument" while setUint8() reports nothing at all. Drop those tests. FunctionCallbackInfo::operator[] returns Undefined for an out-of-range index, which is exactly the value these validators reject, so each missing argument now produces the error its explicit undefined counterpart produces. The documentation already describes this behavior: the signatures are ffi.getInt8(pointer[, offset]), ffi.setInt8(pointer, offset, value) and ffi.toBuffer(pointer, length[, copy]), and the getters are documented to return a number or a bigint. ExportBytes() carried the same two tests. They are unreachable through the public API because exportBytes is not exported and its three callers all validate len in JavaScript first, but they are the same shape. Signed-off-by: Soul Lee <alus20x@gmail.com>
8926b4d to
7ce4938
Compare
|
Opened the follow-up PR: #65842. It switches the two type-check branches to |
Commit Queue failedThe pull request was removed from the Commit Queue and labeled
commit-queue-failed
Full Commit Queue output |
|
Re-added ✔ Last GitHub CI successful
ℹ Last Full PR CI on 2026-09-06T11:27:34Z: https://ci.nodejs.org/job/node-test-pull-request/77074/
⠋ Querying data for job/node-test-pull-request/77074/ ✔ Last Jenkins CI successful
--------------------------------------------------------------------------------
✔ This PR should be ready to land, do you want to continue? YesIf |
|
Landed in 7147a3e |
ffi.getInt8()throughffi.getFloat64(),ffi.setInt8()throughffi.setFloat64(),ffi.toBuffer()andffi.toArrayBuffer()returnundefinedinstead of throwing when a required argument is omitted, so a call that read or wrote nothing cannot be told apart from one that read a zero byte.GetValidatedPointerAddress()andGetValidatedSize()already reject the same argument when it is passed explicitly asundefined. Theargs.Length()test in front of them short-circuits the call and returnsNothingwithout scheduling an exception. These six are the only tests insrc/whereargs.Length()can skip a call that throws; the only otherLength()tests that guard a call at all guardBuffer::HasInstance(), which cannot throw.Drop those tests.
FunctionCallbackInfo::operator[]returnsUndefinedfor an out-of-range index, which is exactly the value these validators reject, so each missing argument now produces the error its explicitundefinedcounterpart produces. The tests that guard an explicit throw, such as the ones inToString()andGetRawPointer(), are left alone.ExportBytes()carried the same two tests. They are unreachable through the public API becauseexportBytesis not exported and its three callers all validatelenin JavaScript first, but they are the same shape.No documentation change is needed:
doc/api/ffi.mdalready lists these arguments as required.Fixes: #65499
Refs: #62072
Refs: #62858
Refs: #65842